package com.dayuanit.util;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.util.LinkedList;
import java.util.Properties;

public class DateBaseUtil{
	
	private static LinkedList<Connection> conns = new LinkedList<Connection>();
	private static final int COUNT_CONNS = 2;

	static {
		try {
			Class.forName("com.mysql.jdbc.Driver");
			InputStream is = DateBaseUtil.class.getClassLoader().getResourceAsStream("jdbc.properties");
			Properties pro = new Properties();
			pro.load(is);
			String url = pro.getProperty("jdbc.url");
			String username = pro.getProperty("jdbc.user");
			String pwd = pro.getProperty("jdbc.pwd");
			
			
			for (int i=0; i<COUNT_CONNS; i++) {
				Connection conn = DriverManager.getConnection(url, username, pwd);
				conns.offerLast(conn);
			}
			
			System.out.println("connection success");
			
		} catch(Exception e) {
			e.printStackTrace();
		}

		
	}

	public static synchronized Connection getConn() {
		return conns.pollFirst();
	}
	
	public static synchronized boolean close(Connection conn) {
		return conns.offerLast(conn);
	}
}